################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Data
#Part VI: Replacement Rates
#
#Verena Fetscher
#July 2022
####################

rm(list=ls())

##########################
#Load Data
##########################

load("DataFile_06_combined.Rda")
load("Benefits.Rda")


##########################
#Import replacement rate data
##########################

#https://stats.oecd.org/Index.aspx?DataSetCode=FIXINCLSA#
#Accessed 11-2017

Ins_single<-read.csv("TaxBen_OECD_single.csv", header = TRUE, sep = ",", quote = "\"",
                     dec = ".")
names(Ins_single) <- c("COU","Country","FAM","Family.type","CHI","Number.of.children", 
                       "ER1","First.earner","ER2","Second.earner","VAR","Variable", 
                       "EMP","Employment.status","YEA","Year","Unit.Code","Unit",
                       "PowerCode.Code","PowerCode","Reference.Period.Code",
                       "Reference.Period","Value","Flag.Codes","Flags")

Ins_married<-read.csv("TaxBen_OECD_married67.csv", header = TRUE, sep = ",", quote = "\"",
                      dec = ".")
names(Ins_married) <- c("COU","Country","FAM","Family.type","CHI","Number.of.children" ,
                        "ER1","First.earner","ER2","Second.earner","VAR","Variable",
                        "EMP","Employment.status","YEA","Year","Unit.Code","Unit",
                        "PowerCode.Code","PowerCode","Reference.Period.Code",
                        "Reference.Period","Value", "Flag.Codes","Flags" )

##########################
#Extract indicators from data file for all countries
##########################

#single earner, no children
single<-data.frame(Ins_single$Country[Ins_single$Employment.status=="Employed"&
                                        Ins_single$Variable=="Gross Income"],
                   Ins_single$First.earner[Ins_single$Employment.status=="Employed"&
                                             Ins_single$Variable=="Gross Income"],
                   Ins_single$Year[Ins_single$Employment.status=="Employed"&
                                     Ins_single$Variable=="Gross Income"],
                   Ins_single$Value[Ins_single$Employment.status=="Employed"&
                                      Ins_single$Variable=="Gross Income"],
                   Ins_single$Value[Ins_single$Employment.status=="Employed"&
                                      Ins_single$Variable=="Net Income"],
                   Ins_single$Value[Ins_single$Employment.status=="Employed"&
                                      Ins_single$Variable=="Income Tax"],
                   Ins_single$Value[Ins_single$Employment.status=="Unemployed"&
                                      Ins_single$Variable=="Net Income"])

names(single) <- c("Country","AW","Year","Gross","Net","Tax","Unemployment")
single$AW<-as.numeric(gsub("% of AW", "", single$AW))


#married, two children, spouse earns 67%
married<-data.frame(Ins_married$Country[Ins_married$Employment.status=="Employed"&
                                          Ins_married$Variable=="Gross Income"],
                    Ins_married$First.earner[Ins_married$Employment.status=="Employed"&
                                               Ins_married$Variable=="Gross Income"],
                    Ins_married$Year[Ins_married$Employment.status=="Employed"&
                                       Ins_married$Variable=="Gross Income"],
                    Ins_married$Value[Ins_married$Employment.status=="Employed"&
                                        Ins_married$Variable=="Gross Income"],
                    Ins_married$Value[Ins_married$Employment.status=="Employed"&
                                        Ins_married$Variable=="Net Income"],
                    Ins_married$Value[Ins_married$Employment.status=="Employed"&
                                        Ins_married$Variable=="Income Tax"],
                    Ins_married$Value[Ins_married$Employment.status=="Unemployed"&
                                        Ins_married$Variable=="Net Income"])

names(married) <- c("Country","AW","Year","Gross","Net","Tax","Unemployment")
married$AW<-as.numeric(gsub("% of AW", "", married$AW))
##########################


#Remove full data file
rm(Ins_married,Ins_single)


#Generate replacement Rate
single$RR<-single$Unemployment/single$Net
married$RR<-married$Unemployment/married$Net

single$famtype<-"single"
married$famtype<-"married"

replacement<-rbind(single,married)


##########################
#Save data frame
##########################

#Replacement rates
save(replacement,file="replacement.Rda")


##########################
#Calculation for main data frame
##########################

#Replacement rate 200% AW

married %>%
  group_by(Country,Year) %>%
  filter(AW==200) %>%
  summarize(marriedRR = mean(RR)) -> married200

married200<-data.frame(married200)

single %>%
  group_by(Country,Year) %>%
  filter(AW==200) %>%
  summarize(singleRR = mean(RR)) -> single200

single200<-data.frame(single200)



combined200<-cbind(single200,married200$marriedRR)
names(combined200)[names(combined200) == "married200$marriedRR"] <- 
  "marriedRR"
combined200$RR<-(combined200$singleRR+combined200$marriedRR)/2

rm(married200,single200)


#Replacement rate 100% AW
married %>%
  group_by(Country,Year) %>%
  filter(AW==100) %>%
  summarize(marriedRR = mean(RR)) -> marriedAW

marriedAW<-data.frame(marriedAW)

single %>%
  group_by(Country,Year) %>%
  filter(AW==100) %>%
  summarize(singleRR = mean(RR)) -> singleAW

singleAW<-data.frame(singleAW)



combinedAW<-cbind(singleAW,marriedAW$marriedRR)
names(combinedAW)[names(combinedAW) == "marriedAW$marriedRR"] <- 
  "marriedRR"
combinedAW$RR<-(combinedAW$singleRR+combinedAW$marriedRR)/2

rm(singleAW,marriedAW)



##########################
#Add to data frame
##########################

data$benLev[data$cntry=="AT"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Austria"]
data$benLev[data$cntry=="AT"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Austria"]
data$benLev[data$cntry=="AT"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Austria"]
data$benLev[data$cntry=="AT"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Austria"]
data$benLev[data$cntry=="AT"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Austria"]
data$benLev[data$cntry=="AT"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Austria"]
data$benLev[data$cntry=="AT"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Austria"]

data$benLevAW[data$cntry=="AT"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Austria"]
data$benLevAW[data$cntry=="AT"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Austria"]
data$benLevAW[data$cntry=="AT"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Austria"]
data$benLevAW[data$cntry=="AT"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Austria"]
data$benLevAW[data$cntry=="AT"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Austria"]
data$benLevAW[data$cntry=="AT"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Austria"]
data$benLevAW[data$cntry=="AT"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Austria"]


data$benLev[data$cntry=="BE"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Belgium"]
data$benLev[data$cntry=="BE"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Belgium"]
data$benLev[data$cntry=="BE"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Belgium"]
data$benLev[data$cntry=="BE"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Belgium"]
data$benLev[data$cntry=="BE"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Belgium"]
data$benLev[data$cntry=="BE"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Belgium"]
data$benLev[data$cntry=="BE"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Belgium"]

data$benLevAW[data$cntry=="BE"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Belgium"]
data$benLevAW[data$cntry=="BE"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Belgium"]
data$benLevAW[data$cntry=="BE"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Belgium"]
data$benLevAW[data$cntry=="BE"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Belgium"]
data$benLevAW[data$cntry=="BE"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Belgium"]
data$benLevAW[data$cntry=="BE"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Belgium"]
data$benLevAW[data$cntry=="BE"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Belgium"]


data$benLev[data$cntry=="DK"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Denmark"]
data$benLev[data$cntry=="DK"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Denmark"]
data$benLev[data$cntry=="DK"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Denmark"]
data$benLev[data$cntry=="DK"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Denmark"]
data$benLev[data$cntry=="DK"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Denmark"]
data$benLev[data$cntry=="DK"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Denmark"]
data$benLev[data$cntry=="DK"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Denmark"]

data$benLevAW[data$cntry=="DK"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Denmark"]
data$benLevAW[data$cntry=="DK"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Denmark"]
data$benLevAW[data$cntry=="DK"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Denmark"]
data$benLevAW[data$cntry=="DK"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Denmark"]
data$benLevAW[data$cntry=="DK"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Denmark"]
data$benLevAW[data$cntry=="DK"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Denmark"]
data$benLevAW[data$cntry=="DK"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Denmark"]


data$benLev[data$cntry=="FI"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Finland"]
data$benLev[data$cntry=="FI"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Finland"]
data$benLev[data$cntry=="FI"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Finland"]
data$benLev[data$cntry=="FI"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Finland"]
data$benLev[data$cntry=="FI"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Finland"]
data$benLev[data$cntry=="FI"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Finland"]
data$benLev[data$cntry=="FI"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Finland"]

data$benLevAW[data$cntry=="FI"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Finland"]
data$benLevAW[data$cntry=="FI"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Finland"]
data$benLevAW[data$cntry=="FI"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Finland"]
data$benLevAW[data$cntry=="FI"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Finland"]
data$benLevAW[data$cntry=="FI"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Finland"]
data$benLevAW[data$cntry=="FI"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Finland"]
data$benLevAW[data$cntry=="FI"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Finland"]


data$benLev[data$cntry=="FR"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="France"]
data$benLev[data$cntry=="FR"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="France"]
data$benLev[data$cntry=="FR"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="France"]
data$benLev[data$cntry=="FR"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="France"]
data$benLev[data$cntry=="FR"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="France"]
data$benLev[data$cntry=="FR"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="France"]
data$benLev[data$cntry=="FR"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="France"]

data$benLevAW[data$cntry=="FR"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="France"]
data$benLevAW[data$cntry=="FR"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="France"]
data$benLevAW[data$cntry=="FR"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="France"]
data$benLevAW[data$cntry=="FR"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="France"]
data$benLevAW[data$cntry=="FR"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="France"]
data$benLevAW[data$cntry=="FR"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="France"]
data$benLevAW[data$cntry=="FR"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="France"]


data$benLev[data$cntry=="DE"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Germany"]
data$benLev[data$cntry=="DE"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Germany"]
data$benLev[data$cntry=="DE"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Germany"]
data$benLev[data$cntry=="DE"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Germany"]
data$benLev[data$cntry=="DE"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Germany"]
data$benLev[data$cntry=="DE"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Germany"]
data$benLev[data$cntry=="DE"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Germany"]

data$benLevAW[data$cntry=="DE"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Germany"]
data$benLevAW[data$cntry=="DE"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Germany"]
data$benLevAW[data$cntry=="DE"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Germany"]
data$benLevAW[data$cntry=="DE"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Germany"]
data$benLevAW[data$cntry=="DE"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Germany"]
data$benLevAW[data$cntry=="DE"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Germany"]
data$benLevAW[data$cntry=="DE"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Germany"]


data$benLev[data$cntry=="IE"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Ireland"]
data$benLev[data$cntry=="IE"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Ireland"]
data$benLev[data$cntry=="IE"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Ireland"]
data$benLev[data$cntry=="IE"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Ireland"]
data$benLev[data$cntry=="IE"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Ireland"]
data$benLev[data$cntry=="IE"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Ireland"]
data$benLev[data$cntry=="IE"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Ireland"]

data$benLevAW[data$cntry=="IE"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Ireland"]
data$benLevAW[data$cntry=="IE"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Ireland"]
data$benLevAW[data$cntry=="IE"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Ireland"]
data$benLevAW[data$cntry=="IE"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Ireland"]
data$benLevAW[data$cntry=="IE"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Ireland"]
data$benLevAW[data$cntry=="IE"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Ireland"]
data$benLevAW[data$cntry=="IE"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Ireland"]


data$benLev[data$cntry=="IT"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Italy"]
data$benLev[data$cntry=="IT"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Italy"]
data$benLev[data$cntry=="IT"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Italy"]
data$benLev[data$cntry=="IT"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Italy"]
data$benLev[data$cntry=="IT"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Italy"]
data$benLev[data$cntry=="IT"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Italy"]
data$benLev[data$cntry=="IT"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Italy"]

data$benLevAW[data$cntry=="IT"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Italy"]
data$benLevAW[data$cntry=="IT"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Italy"]
data$benLevAW[data$cntry=="IT"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Italy"]
data$benLevAW[data$cntry=="IT"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Italy"]
data$benLevAW[data$cntry=="IT"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Italy"]
data$benLevAW[data$cntry=="IT"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Italy"]
data$benLevAW[data$cntry=="IT"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Italy"]


data$benLev[data$cntry=="NL"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Netherlands"]
data$benLev[data$cntry=="NL"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Netherlands"]
data$benLev[data$cntry=="NL"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Netherlands"]
data$benLev[data$cntry=="NL"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Netherlands"]
data$benLev[data$cntry=="NL"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Netherlands"]
data$benLev[data$cntry=="NL"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Netherlands"]
data$benLev[data$cntry=="NL"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Netherlands"]

data$benLevAW[data$cntry=="NL"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Netherlands"]
data$benLevAW[data$cntry=="NL"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Netherlands"]
data$benLevAW[data$cntry=="NL"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Netherlands"]
data$benLevAW[data$cntry=="NL"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Netherlands"]
data$benLevAW[data$cntry=="NL"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Netherlands"]
data$benLevAW[data$cntry=="NL"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Netherlands"]
data$benLevAW[data$cntry=="NL"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Netherlands"]


data$benLev[data$cntry=="NO"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Norway"]
data$benLev[data$cntry=="NO"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Norway"]
data$benLev[data$cntry=="NO"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Norway"]
data$benLev[data$cntry=="NO"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Norway"]
data$benLev[data$cntry=="NO"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Norway"]
data$benLev[data$cntry=="NO"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Norway"]
data$benLev[data$cntry=="NO"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Norway"]

data$benLevAW[data$cntry=="NO"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Norway"]
data$benLevAW[data$cntry=="NO"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Norway"]
data$benLevAW[data$cntry=="NO"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Norway"]
data$benLevAW[data$cntry=="NO"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Norway"]
data$benLevAW[data$cntry=="NO"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Norway"]
data$benLevAW[data$cntry=="NO"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Norway"]
data$benLevAW[data$cntry=="NO"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Norway"]


data$benLev[data$cntry=="PT"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Portugal"]
data$benLev[data$cntry=="PT"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Portugal"]
data$benLev[data$cntry=="PT"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Portugal"]
data$benLev[data$cntry=="PT"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Portugal"]
data$benLev[data$cntry=="PT"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Portugal"]
data$benLev[data$cntry=="PT"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Portugal"]
data$benLev[data$cntry=="PT"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Portugal"]

data$benLevAW[data$cntry=="PT"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Portugal"]
data$benLevAW[data$cntry=="PT"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Portugal"]
data$benLevAW[data$cntry=="PT"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Portugal"]
data$benLevAW[data$cntry=="PT"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Portugal"]
data$benLevAW[data$cntry=="PT"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Portugal"]
data$benLevAW[data$cntry=="PT"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Portugal"]
data$benLevAW[data$cntry=="PT"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Portugal"]

data$benLev[data$cntry=="ES"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Spain"]
data$benLev[data$cntry=="ES"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Spain"]
data$benLev[data$cntry=="ES"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Spain"]
data$benLev[data$cntry=="ES"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Spain"]
data$benLev[data$cntry=="ES"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Spain"]
data$benLev[data$cntry=="ES"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Spain"]
data$benLev[data$cntry=="ES"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Spain"]

data$benLevAW[data$cntry=="ES"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Spain"]
data$benLevAW[data$cntry=="ES"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Spain"]
data$benLevAW[data$cntry=="ES"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Spain"]
data$benLevAW[data$cntry=="ES"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Spain"]
data$benLevAW[data$cntry=="ES"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Spain"]
data$benLevAW[data$cntry=="ES"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Spain"]
data$benLevAW[data$cntry=="ES"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Spain"]

data$benLev[data$cntry=="SE"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Sweden"]
data$benLev[data$cntry=="SE"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Sweden"]
data$benLev[data$cntry=="SE"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Sweden"]
data$benLev[data$cntry=="SE"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Sweden"]
data$benLev[data$cntry=="SE"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Sweden"]
data$benLev[data$cntry=="SE"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Sweden"]
data$benLev[data$cntry=="SE"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Sweden"]

data$benLevAW[data$cntry=="SE"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Sweden"]
data$benLevAW[data$cntry=="SE"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Sweden"]
data$benLevAW[data$cntry=="SE"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Sweden"]
data$benLevAW[data$cntry=="SE"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Sweden"]
data$benLevAW[data$cntry=="SE"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Sweden"]
data$benLevAW[data$cntry=="SE"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Sweden"]
data$benLevAW[data$cntry=="SE"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Sweden"]


data$benLev[data$cntry=="CH"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="Switzerland"]
data$benLev[data$cntry=="CH"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="Switzerland"]
data$benLev[data$cntry=="CH"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="Switzerland"]
data$benLev[data$cntry=="CH"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="Switzerland"]
data$benLev[data$cntry=="CH"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="Switzerland"]
data$benLev[data$cntry=="CH"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="Switzerland"]
data$benLev[data$cntry=="CH"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="Switzerland"]

data$benLevAW[data$cntry=="CH"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="Switzerland"]
data$benLevAW[data$cntry=="CH"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="Switzerland"]
data$benLevAW[data$cntry=="CH"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="Switzerland"]
data$benLevAW[data$cntry=="CH"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="Switzerland"]
data$benLevAW[data$cntry=="CH"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="Switzerland"]
data$benLevAW[data$cntry=="CH"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="Switzerland"]
data$benLevAW[data$cntry=="CH"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="Switzerland"]


data$benLev[data$cntry=="GB"&data$essround==1]<-
  combined200$RR[combined200$Year==2002&combined200$Country=="United Kingdom"]
data$benLev[data$cntry=="GB"&data$essround==2]<-
  combined200$RR[combined200$Year==2004&combined200$Country=="United Kingdom"]
data$benLev[data$cntry=="GB"&data$essround==3]<-
  combined200$RR[combined200$Year==2006&combined200$Country=="United Kingdom"]
data$benLev[data$cntry=="GB"&data$essround==4]<-
  combined200$RR[combined200$Year==2008&combined200$Country=="United Kingdom"]
data$benLev[data$cntry=="GB"&data$essround==5]<-
  combined200$RR[combined200$Year==2010&combined200$Country=="United Kingdom"]
data$benLev[data$cntry=="GB"&data$essround==6]<-
  combined200$RR[combined200$Year==2012&combined200$Country=="United Kingdom"]
data$benLev[data$cntry=="GB"&data$essround==7]<-
  combined200$RR[combined200$Year==2014&combined200$Country=="United Kingdom"]

data$benLevAW[data$cntry=="GB"&data$essround==1]<-
  combinedAW$RR[combinedAW$Year==2002&combinedAW$Country=="United Kingdom"]
data$benLevAW[data$cntry=="GB"&data$essround==2]<-
  combinedAW$RR[combinedAW$Year==2004&combinedAW$Country=="United Kingdom"]
data$benLevAW[data$cntry=="GB"&data$essround==3]<-
  combinedAW$RR[combinedAW$Year==2006&combinedAW$Country=="United Kingdom"]
data$benLevAW[data$cntry=="GB"&data$essround==4]<-
  combinedAW$RR[combinedAW$Year==2008&combinedAW$Country=="United Kingdom"]
data$benLevAW[data$cntry=="GB"&data$essround==5]<-
  combinedAW$RR[combinedAW$Year==2010&combinedAW$Country=="United Kingdom"]
data$benLevAW[data$cntry=="GB"&data$essround==6]<-
  combinedAW$RR[combinedAW$Year==2012&combinedAW$Country=="United Kingdom"]
data$benLevAW[data$cntry=="GB"&data$essround==7]<-
  combinedAW$RR[combinedAW$Year==2014&combinedAW$Country=="United Kingdom"]





##########################
#Save data frame
##########################

#Combine ESS with replacement rate
save(data,file="DataFile_07_Replacement.Rda")
